package com.atguigu.mq.listener;


import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MessageListener {
    //监听指定名称的队列(是个数组)
    @RabbitListener(queues = "atguigu.queue.simple")
    //消息是字符串,用字符串接收,
    public void mesHandler(String str , Message message, Channel channel){

        System.out.println(str);


        //getbody消息字符串对应的二进制属性
        byte[] body = message.getBody();
        System.out.println(new String(body));


        //消息的属性如:长度字符编码,内容类型,优先级
        MessageProperties messageProperties = message.getMessageProperties();
        System.out.println(messageProperties);


//        helloworld
//        helloworld
//        MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8,
//                contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false,
//                receivedExchange=, receivedRoutingKey=atguigu.queue.simple,
//                deliveryTag=1, //消息唯一标识相当于数据库的id
//                consumerTag=amq.ctag-WjtMspBRkcYzNaqCm-5vgw, consumerQueue=atguigu.queue.simple]


    }


    //==============================
    //工作队列模式的消费者是竞争关系
    @RabbitListener(queues = "atguigu.queue.work")
    public void workHandler1(String str){
        System.out.println("work1"+str);
    }
    @RabbitListener(queues = "atguigu.queue.work")
    public void workHandler2(String str){
        System.out.println("work2"+str);
    }


    //=====================


    @RabbitListener(queues = "atguigu.queue.fanout1")
    public void fanoutHandler1(String str){
        System.out.println("fanout="+str);
    }
    @RabbitListener(queues = "atguigu.queue.fanout2")
    public void fanoutHandler2(String str){
        System.out.println("fanout="+str);
    }


    //=====================

    @RabbitListener(queues = "atguigu.queue.direct")
    public void routingHandler(String str){
        System.out.println("direct="+str);
    }


    //=====================

    @RabbitListener(queues = "atguigu.queue.message")
    public void topicHandler1(String str){
        System.out.println("message="+str);
    }
    @RabbitListener(queues = "atguigu.queue.order")
    public void topicHandler2(String str){
        System.out.println("order="+str);
    }




}
